package com.hoshiicloud.vericenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.vericenter.dao.SysMessagesMapper;
import com.hoshiicloud.vericenter.entity.SysMessages;
import com.hoshiicloud.vericenter.feign.IdGeneratorService;
import com.hoshiicloud.vericenter.result.Response;
import com.hoshiicloud.vericenter.service.ISysMessagesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 短信 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-05-20
 */
@Service
public class SysMessagesServiceImpl extends ServiceImpl<SysMessagesMapper, SysMessages> implements ISysMessagesService {
	
	@Autowired
	private SysMessagesMapper sysMessagesMapper;
	
	@Autowired
    private IdGeneratorService idGeneratorService;

	@Override
	public Response batchInsert(List<SysMessages> list) {
		int row= sysMessagesMapper.batchInsert(list);
		return Response.data(row);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "SysMessages" }, allEntries = true)
	@Logable(authName = "验证中心-短信信息管理-新增短信信息")
	public Response insert(SysMessages param){
		String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("id服务失败"));
        }
        param.setId(Long.parseLong(id));
        param.setSentTime(LocalDateTime.now());
		int count = sysMessagesMapper.insert(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("新增失败"));
		}
		return Response.data(id);
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "SysMessages" }, allEntries = true)
	@Logable(authName = "验证中心-短信信息管理-修改短信信息")
	public Response updateDataById(SysMessages param){
		int count = sysMessagesMapper.updateById(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("修改失败"));
		}
		return Response.success("修改成功");
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "SysMessages" }, allEntries = true)
	@Logable(authName = "验证中心-短信信息管理-删除短信信息")
	public Response deleteDataById(String id){
		int count = sysMessagesMapper.deleteById(id);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("删除失败"));
		} else {
			return Response.success("删除成功");
		}
	}
	@Override
	@Cacheable(cacheNames = "SysMessages", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "验证中心-短信信息管理-查询短信信息详情")
	public Response getDetail(String id){
		SysMessages data = sysMessagesMapper.selectById(id);
		if (data == null) {
			return Response.failed("数据不存在");
		} else {
			return Response.data(data);
		}
	}
	@Override
	@Cacheable(cacheNames = "SysMessages", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "验证中心-短信信息管理-查询短信信息列表")
	public Response getList(QueryWrapper<SysMessages> queryWrapper){
		return Response.data(sysMessagesMapper.selectList(queryWrapper));
	}
	@Override
	@Cacheable(cacheNames = "SysMessages", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "验证中心-短信信息管理-分页查询短信信息")
	public Response getPage(IPage<SysMessages> page, QueryWrapper<SysMessages> queryWrapper){
		return Response.data(sysMessagesMapper.selectPage(page, queryWrapper));
	}
}
